﻿using System.Text;
using NAnt.Core.Attributes;
using Yahoo.Yui.Compressor;

namespace ECM7.NantContrib.Tasks.Compressor
{
	/// <summary>
	/// Минимизация CSS
	/// </summary>
	[TaskName("minify-css")]
	public class MinifyCssTask : BaseMinifyTask<CssCompressionType>
	{
		#region properties

		/// <summary>
		/// Тип сжатия CSS
		/// </summary>
		[TaskAttribute("compression-type", Required = false)]
		public string CssCompressionTypeString { get; set; }

		#endregion

		#region BaseMinifyTask Implementation

		/// <summary>
		/// Тип обрабатываемых файлов для отборажения в логе
		/// </summary>
		protected override string FileTypeString
		{
			get { return "Css"; }
		}

		/// <summary>
		/// Сжатие файлов
		/// </summary>
		/// <param name="content">Содержание минимизируемого файла</param>
		/// <param name="compressionType">Специфические параметры сжатия</param>
		/// <param name="encoding">Кодировка</param>
		protected override string CompressFile(string content, CssCompressionType compressionType, Encoding encoding)
		{
			return CssCompressor.Compress(content, WithLineBreaks ? 140 : 0, compressionType);
		}

		/// <summary>
		/// Инициализация
		/// </summary>
		protected override CssCompressionType GetCustomParams()
		{
			string cssct = (CssCompressionTypeString ?? string.Empty).Trim().ToLowerInvariant();

			switch (cssct)
			{
				case "stockyuicompressor":
				case "stock-yui-compressor":
					return CssCompressionType.StockYuiCompressor;
				case "michaelashsregexenhancements":
				case "michael-ash-sregex-enhancements":
					return CssCompressionType.MichaelAshRegexEnhancements;
				case "hybrid":
					return CssCompressionType.Hybrid;
				default:
					return CssCompressionType.StockYuiCompressor;
			}
		}

		#endregion
	}
}
